<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>出库信息</title>
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.4.4.min.js"	type="text/javascript"></script>
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.form.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<script src="${BasePath}/res/js/common/jquery.validate.method.js" type="text/javascript"></script>
<script src="${Plugins}/datepicker/WdatePicker.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/ajaxfileupload.js" type="text/javascript"></script>

<c:set var="isAddPage" value="${stockOutViewType == 1}"/>
<c:set var="isAduitPage" value="${stockOutViewType == 2}"/>
<c:set var="isDetailPage" value="${stockOutViewType == 3}"/>
<c:set var="isPackageImportPage" value="${stockOutViewType == 5}"/>
<c:set var="isPurReturnImportPage" value="${stockOutViewType == 7}"/>
<c:set var="isDefectReturnImportPage" value="${stockOutViewType == 8}"/>
<c:set var="isPackageReturnImportPage" value="${stockOutViewType == 10}"/>
<c:set var="isTransferImportPage" value="${stockOutViewType == 12}"/>
<c:set var="isUserFetch" value="${stockOut.productFetchType == 0}"/>
<c:set var="isAduitedData" value="${stockOut.stockOutState == 1}"/>


<style type="text/css">
	
    .l-table-edit {}
    .l-table-edit-td{ padding:5px;}
    .l-button-submit,.l-button-test{height:25px; width:100px; float:left; margin-left:10px; padding-bottom:2px;}
    .l-verify-tip{ left:230px; top:120px;}
    .p-tab .s-lab{font-size:6px;font-family:"微软雅黑",​"宋体",​Arial,​sans-serif;}
	input.l-textarea{width:170px;height:20px;font-size:12px;}
	span{font-size:13px;}
	.fontSize {font-size: 13px;}
	.navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
	select{margin-right: 10px;width: 170px;}
	#searchbar{
   		margin-bottom: 10px;
   		margin-left:15px;
  	}
	ul {list-style:none;border: 1px solid #DDDDDD;width:400px; float:left;}
	.li_ls{
		float:left; 
		width:30%; 
		margin:5px; 
		word-break:keep-all;/* 不换行 */
		white-space:nowrap;/* 不换行 */
		overflow:hidden;
		text-overflow:ellipsis;
	}
	.l-button-search {
		width: 50px;
		margin-left: 10px;
		padding-bottom: 2px;
		display: inline;
	}	
	.inputSize{width: 170px;}
</style>
</head>
<body style="padding:10px">
<div class="navbar">
	<span>您当前的位置：</span>
	<c:choose>
		<c:when test="${!isAduitedData and isAduitPage}">
			出入库管理 &gt;商品出库管理>&gt;出库
		</c:when>
		<c:when test="${isPackageImportPage}">
			多渠道订单管理 &gt;商品包裹单>&gt;生成出库单
		</c:when>
		<c:when test="${isPackageReturnImportPage}">
			多渠道订单管理 &gt;订单退换货管理>&gt;生成出库单
		</c:when>
		<c:when test="${isPurReturnImportPage}">
			商品管理 &gt;采购退货管理>&gt;生成出库单
		</c:when>
		<c:when test="${isDefectReturnImportPage}">
			商品管理 &gt;次品退货管理>&gt;退货出库
		</c:when>
		<c:when test="${isTransferImportPage}">
			商品库存管理 &gt;库存调拨>&gt;生成出库单
		</c:when>
		<c:when test="${isAddPage}">
			出入库管理 &gt;商品出库管理>&gt;新增
		</c:when>
		<c:otherwise>
			出入库管理 &gt;商品出库管理>&gt;详细
		</c:otherwise>
	</c:choose>
</div>
<form name="form1" method="post" action="approveStockIn.do" id="form1" onsubmit="return setGridData()">
<input type="hidden" id="id" name="id" value="${stockOut.id}"> 
<br>
<table border=0 cellpadding="0" cellspacing="0" class="l-table-edit p-tab" style="height:100%;width:100%">
    <tr style="height: 5%">
        <td>
        	<table  style="width: 80%" border=0> 
        		<tr>
					<c:if test="${!isAddPage}">
						<td style="width: 10%">
							<span>出库单号&nbsp;:</span>
						</td>
        				<td  style="width: 20%">
							<span id="stockOutNo">${stockOut.stockOutNo}</span>
						</td>
					</c:if>
					<td  style="width: 10%">
        				<span>出库原因&nbsp;:</span>
        			</td>
        			<td  style="width: 20%">
		    	 		<span>
		    	 			<c:choose>
								<c:when test="${isAddPage}">
									<select  class="l-textarea"  id="stockOutType"  name="stockOutType">
							            <option value="-1" <c:if test="${stockOut.stockOutType==-1}">selected="selected"</c:if>>包裹销售出库</option>
							            <option value="-2" <c:if test="${stockOut.stockOutType==-2}">selected="selected"</c:if>>订单退货出库</option>
							    	 	<option value="-3" <c:if test="${stockOut.stockOutType==-3}">selected="selected"</c:if>>次品退货出库</option>
							    		<option value="-4" <c:if test="${stockOut.stockOutType==-4}">selected="selected"</c:if>>订单退换货出库</option>
 							    		<option value="-5" <c:if test="${stockOut.stockOutType==-5}">selected="selected"</c:if>>调拨出库</option>
							    	 </select>
								</c:when>
								<c:otherwise>
									<c:choose> 
										  <c:when test="${stockOut.stockOutType==-1}">包裹销售出库</c:when> 
										  <c:when test="${stockOut.stockOutType==-2}">采购退换货出库</c:when> 
										  <c:when test="${stockOut.stockOutType==-3}">次品退货出库</c:when>
										  <c:when test="${stockOut.stockOutType==-4}">订单退换货出库</c:when> 
										  <c:when test="${stockOut.stockOutType==-5}">调拨出库</c:when> 
										  <c:otherwise></c:otherwise> 
									</c:choose> 
								</c:otherwise>
							</c:choose>
							<span id="stockOutType" style="display:none;">${stockOut.stockOutType}</span>
			    	 		<span id="stockOutTypeNo" style="display:none;">${stockOut.stockOutTypeNo}</span>
			    	 	</span>
        			</td>
        			<td  style="width: 10%">
						<c:if test = "${isAduitPage}">
							是否自取
						</c:if>
					</td>
        			<td  style="width: 20%">
						<c:if test = "${isAduitPage}">
							<input id = 'fetchbox' type="checkbox"  value = '1'>
						</c:if>
					</td>
        		</tr>
        		<tr style="height: 9px">
        			<td colspan="6"></td>
        		</tr>
        		<tr>
        			<td>
        			    <span>收货人&nbsp;:</span>
        			</td>
        			<td>
        				<span>
		    	 			<c:choose>
								<c:when test="${isAddPage}">
									<input value="${stockOut.receiverName}"name="receiverName" id = "receiverName" />
								</c:when>
								<c:otherwise>
									<span id="receiverName">${stockOut.receiverName}</span>
								</c:otherwise>
							</c:choose>
			    	 	</span>
        			</td>
        			<td>
        				<span>电话&nbsp;:</span>
        			</td>
        			<td>
        				<span>
		    	 			<c:choose>
								<c:when test="${isAddPage}">
									<input value="${stockOut.receiverTel}"name="receiverTel" id = "receiverTel" />
								</c:when>
								<c:otherwise>
									<span id="receiverTel">${stockOut.receiverTel}</span>
								</c:otherwise>
							</c:choose>
			    	 	</span>
        			</td>
        			<td>
        			    <span>地址&nbsp;:</span>
        			</td>
        			<td>
        				<span>
		    	 			<c:choose>
								<c:when test="${isAddPage}">
									<input value="${stockOut.receiverAddr}"name="receiverAddr" id = "receiverAddr" />
								</c:when>
								<c:otherwise>
									<span id="receiverAddr">${stockOut.receiverAddr}</span>
								</c:otherwise>
							</c:choose>
			    	 	</span>
        			</td>
        		</tr>
        		<tr style="height: 9px">
        			<td colspan="6"></td>
        		</tr>
        		<c:if test="${isAduitPage || (isDetailPage && !isUserFetch)}">
	        		<tr  id = "logistics"  >
	        			<td>
	        				<span>物流公司&nbsp;:</span>
						</td>
        				<td>
	        				<span>
	        					<c:choose>
									<c:when test="${isAduitPage}">
										<div id = "logisticsNameDiv">
										<input type="hidden" id="logisticsNo" name = "logisticsNo">
							    	  	<input type="text" id="logisticsName" name = "logisticsName" onclick="selectLogistics()" readonly="readonly"class="l-textarea" validate="{required:true}">
										</div>
									</c:when>
									<c:otherwise>
										<span id="">${stockOut.logisticsName}</span>
									</c:otherwise>
								</c:choose>
	        				</span>
	        			</td>
	 			        <td>
	 			        	<span>物流单号&nbsp;:</span>
	 			        </td>
        				<td colspan="3">
	        				<span>
	        					<c:choose>
									<c:when test="${isAduitPage}">
									<div id = "logisticsOrderDiv">
										<input name="logisticsOrder" id = "logisticsOrder"   class="l-textarea"  validate="{required:true}"/>
									</div>
									</c:when>
									<c:otherwise>
										<span id="">${stockOut.logisticsOrder}</span>
									</c:otherwise>
								</c:choose>
	        				</span>
	 			        </td>
	        		</tr>
        		</c:if>
        	</table>
        	<br>
		</td>
    </tr>
    <tr style="height: 50%">
        <td>
        	<div id="maingrid" style="margin:0; padding:0"></div>
  				<div style="display:none;">
			</div>
        </td>
    </tr>
	<tr style="height: 35%">
		<td style="padding-left:40%;top: 80%">
			<div id = "buttonDiv">
			<c:choose>
				<c:when test="${isAddPage}">
					<input type="button" onclick="addStockOut()" value="新增"id="Button1" class="l-button l-button-submit" /> 
				</c:when>
				<c:when test="${isPackageReturnImportPage || isPackageImportPage || isPurReturnImportPage || isDefectReturnImportPage || isTransferImportPage}">
					<input type="button" onclick="addStockOut()" value="出库"id="Button1" class="l-button l-button-submit" /> 
				</c:when>
				<c:when test="${!isAduitedData and isAduitPage}">
					<input type="submit" value="出库" id="Button1" class="l-button l-button-submit"/>
				</c:when>
			</c:choose>
			</div> 
			<input type="button" value="<c:if test = "${!isAduitedData}">取消</c:if><c:if test = "${isAduitedData}">返回</c:if>" name="tests"  id="tests" class="l-button l-button-test" />
		</td>
    </tr>
</table>
</form>
<div id="importBarCodeDiv" style="display:none"></div>
<script type="text/javascript">


	var basePath="${BasePath}";
	var stockOutNo = "${stockOut.stockOutNo}";
	var transferOutWhNo ="${stockTransfer==null?'':stockTransfer.transferOutWhNo}";
	var transferOutWhName ="${stockTransfer==null?'':stockTransfer.transferOutWhName}";
	var isAddPage = ${isAddPage};
	var isAduitPage = ${isAduitPage};
	var isDetailPage = ${isDetailPage};
	var isPackageImportPage = ${isPackageImportPage};
	var isPackageReturnImportPage = ${isPackageReturnImportPage};
	var isPurReturnImportPage = ${isPurReturnImportPage};
	var isDefectReturnImportPage = ${isDefectReturnImportPage};
	var isTransferImportPage = ${isTransferImportPage};
	var parentTabId = ${tabId}; 
  	var tabId = window.top.tab.getSelectedTabItemID();
	var girdUrl = basePath + "/stockmanage/StockController/ajaxPageFinderStockOutDetail.do?stockOutNo=${stockOut.stockOutNo}";
	var ajaxAddUrl = basePath + "/stockmanage/StockController/addStockOut.do";
	var ajaxApproveUrl = basePath + "/stockmanage/StockController/approveStockOut.do";
	var ajaxUploadUrl = basePath +  "/stockmanage/productBarCodeContrller/checkBarCodeForStockOut.do";
	var barCodeQueryUrl = basePath + '/stockmanage/productBarCodeContrller/pageFinderProductBarCode.do';
	var barCodeDeleteUrl = basePath + '/stockmanage/productBarCodeContrller/deleteProductBarCode.do';
	var purQueryUrl = basePath + '/stockmanage/StockOutPurInfo/toQueryStockOutPurInfo.do';
	var purInfoDeleteUrl = basePath + '/stockmanage/StockOutPurInfo/deleteStockOutPurInfo.do';
	var purInfoSelectUrl = basePath + '/stockmanage/StockOutPurInfo/toAddStockOutPurInfo.do';
	var g = null;
	var columns = null;
	var gridArgs = {
 			pageSize:30,
            width: '100%',
            height: '90%',
            dataType: 'server', 
            dataAction: 'server',
            pagesizeParmName: 'pageSize',
            enabledEdit: true
   };
	var render = function(rowdata, rowindex, value){
		   if(value == '' || value == null){
			return  "<font style='color: blue;font-size: 12px;'>请输入</font>";
		   }
		   return value;
	};
	
	function getColumns(){
		return columns;
	}
	
	function getGirdUrl(){
		return girdUrl;
	}
	
	function operateSuccess(){
		window.top.tab.selectTabItem(parentTabId);
		window.top.tab.reload(parentTabId);
		window.top.tab.removeTabItem(tabId);
	}
	function setColumns(index,columnsBody){
		if(index > columns.length){
			index = columns.length;
		}
		columns[index] = columnsBody;
	}
	// 取消
	$("#tests").click(function() {
			operateSuccess();
	});
	
	$("#fetchbox").click(function (){
		if($("#fetchbox").val() == 0){
			$("#fetchbox").val('1')
   	    	$("#buttonDiv").html("<input type='submit' value='出库' id='Button1' class='l-button l-button-submit'/>");
			$("#logistics").show()
		}else{
			$("#fetchbox").val('0')
   	     	$("#buttonDiv").html("<input type='button' value='出库' id='Button1' class='l-button l-button-submit' onClick='approveStockOut()'/>");
			$("#logistics").hide()
		}
	}); 
	
	
	columns = [
            { display: '货品编号', name: 'prodNo'},
            { display: '商品名称', name: 'commoName'},
            { display: '货品条码', name: 'prodBarCode'},
            { display: '货品规格', name: 'specification',
				render : function(rowdata, rowindex, value) {
					if(rowdata.specName == "" || rowdata.specName == null){
						return value
					}
					return rowdata.specName
			}},
            { display: '仓库', name: 'whName'},
            { display: '供应商名称', name: 'supName'},
            { display: '出库数量', name: 'stockoutQuantity'},
       ];
	
		var getBaseText = function(rowdata, rowindex, value){
		   return "<font style='color: blue;font-size: 12px;'>请输入数量</font>";
		}
		
		function toImportCode(index) {
		      	 $("#importBarCodeDiv").html('<input type="file" id="fileupload" name="barCodeFile" onchange="importBarCode('+index+')"/>');
		      	 g.select(index);
		      	 $("#fileupload").click(); 
		}
		
		//-------------区分页面
	   if(isPackageImportPage){
		   girdUrl = basePath + "/stockmanage/StockController/getPackageProduct.do?prodPackageNo=${stockOut.stockOutTypeNo}";
	   }else if(isTransferImportPage){
		   girdUrl = basePath + "/warehouse/stockTransferController/ajaxPageFinderStockTransferProduct.do?transferNo=${stockOut.stockOutTypeNo}";
		   columns =
		        [
		            { display: '货品编号', name: 'prodNo'},
		            { display: '商品名称', name: 'commoName'},
		            { display: '货品条码', name: 'prodBarCode',render : function(rowdata, rowindex, value) {
		            	rowdata.prodBarCode = rowdata.prodNo;
		            	return rowdata.prodBarCode;
		            }},
		            { display: '货品规格', name: 'specification',
						render : function(rowdata, rowindex, value) {
							if(rowdata.specName == "" || rowdata.specName == null){
								return value
							}
							return rowdata.specName
					}},
					{ display: '仓库', name: 'whName',render : function(rowdata, rowindex, value) {
		            	rowdata.whNo = transferOutWhNo;
		            	rowdata.whName = transferOutWhName;
		            	return  rowdata.whName;
		            }},
		            { display: '供应商名称', name: 'supName'},
		            { display: '出库数量', name: 'stockoutQuantity',render : function(rowdata, rowindex, value) {
		            	rowdata.stockoutQuantity = rowdata.qualifiedQuantity;
		            	return  rowdata.stockoutQuantity;
		            }},
		       ];
	   }else if(isPackageReturnImportPage){
		   columns =
		        [
		            { display: '货品编号', name: 'prodNo'},
		            { display: '商品名称', name: 'commoName',render : function(rowdata, rowindex, value) {
		            	rowdata.commoName = rowdata.prodName;
		            	return  rowdata.commoName;
		            }},
		            { display: '货品条码', name: 'prodBarCode'},
		            { display: '货品规格', name: 'specification',
						render : function(rowdata, rowindex, value) {
							if(rowdata.specName == "" || rowdata.specName == null){
								return value
							}
							return rowdata.specName
					}},
		            { display: '供应商名称', name: 'supName',render : function(rowdata, rowindex, value) {
		            	rowdata.supNo = rowdata.code;
		            	return  value;
		            }},
		            { display: '出库数量', name: 'stockoutQuantity',render : function(rowdata, rowindex, value) {
		            	rowdata.stockoutQuantity = rowdata.total;
		            	return  rowdata.stockoutQuantity;
		            }},
		       ];
			   var wh = { display: '仓库', name: 'whNo',editor:{
		      	  	type:'select',
		    	  	data: getHwData(),
		    	  	valueField:"whNo",
		    	  	textField:"whName"
	     		 },
	            render: function (record, rowindex, value, column){
	         	   if(value == null || value == ''){
	         		  record.whNo =  whData[0]['whNo'];
		          	  record.whName = [0]['whName'];
		          	  return whData[0]['whName'];
	         	   }
	 	           for (var i = 0; i < whData.length; i++){
	 	        	  if (whData[i]['whNo'] == value){
	  	            	   record.whNo =  whData[i]['whNo'];
	  	            	   record.whName =  [i]['whName'];
	  	                   return whData[i]['whName'];
	  	               }
	 	           }
	 	       }
	      	}
			setColumns(columns.length, wh);
			girdUrl = basePath + "/order/channelOrder/returnOrderItemPageList.do?channelOrderId=${stockOut.stockOutTypeNo}";
	   }else if(isPurReturnImportPage){
		   girdUrl = basePath + "/manager/purchaseReturn/orders/purchaseOrderList_OutWarehouse.do?puNo=${stockOut.stockOutTypeNo}";
	   }else if(isDefectReturnImportPage){
		   girdUrl = basePath + "/product/defectiveReplacement/ajaxToPoProductList.do?DefectReplceId=${stockOut.stockOutTypeNo}";
	   }else if(isAduitPage){
			gridArgs.usePager = false;
		    setColumns(columns.length,{display: '发货', name: 'isSupDelivery',render:function(rowdata,rowindex,value){
	            var text = null;
	            switch(value){
		            case 0: text="商城发货"; break;
		            case 1: text="供应商发货"; break;       
		            default : break; 
            	}
            	return text;
       		}});
			setColumns(columns.length,{display: '描述', name: 'prodDesc',editor:{type:'text'},render:render});
			setColumns(columns.length, {display : '操作',width:180 ,name : 'productCodeState',
				render : function(rowdata, rowindex, value) {
					var result = "";
					if(isAduitPage){
						if(value == 1){
							result += "<a href='javascript:deleteImportCode("+rowindex+")'>删除条形码</a>&nbsp;&nbsp;";
							result += "<a href='javascript:queryImportCode("+rowindex+ ")'>查看条形码</a>&nbsp;&nbsp;";
						}else if(value == 2){
							result += "<a href='javascript:deletePurInfo("+rowindex+")'>删除采购单</a>&nbsp;&nbsp;";
							result += "<a href='javascript:queryPurInfo("+rowindex+ ")'>查看采购单</a>&nbsp;&nbsp;";
						}else{
							result += "<a href='javascript:toImportCode("+rowindex+ ")'>导入条形码</a>&nbsp;&nbsp;";
							if($('#stockOutType').html() == -1 || $('#stockOutType').html() == -5 ){//包裹出库或者调拨可以手选采购单
								result += "<a href=javascript:toSelectPur("+rowindex+ ")>关联采购单</a>&nbsp;&nbsp;";
							}
						}
					}
					return result
				}
			});
			var downloadFile = "<a style = 'color:black' href='${BasePath}/stockmanage/productBarCodeContrller/dowanloadTemplet.do'>下载条形码模板</a>";
			gridArgs.toolbar= {items: [{ text:downloadFile, click: function(){}, icon: "down" }]}		
	   }else if(isDetailPage){
		   setColumns(columns.length,{display: '发货', name: 'isSupDelivery',render:function(rowdata,rowindex,value){
	            var text = null;
	            switch(value){
		            case 0: text="商城发货"; break;
		            case 1: text="供应商发货"; break;       
		            default : break; 
           		}
	            return text;
	        }});
		   
			setColumns(columns.length,{display: '描述', name: 'prodDesc'});
			setColumns(columns.length, {display : '操作',width:180 ,name : 'productCodeState',
				render : function(rowdata, rowindex, value) {
					var result = "";
					if(value == 1){
						result += "<a href='javascript:queryImportCode("+rowindex+ ")'>查看条形码</a>&nbsp;&nbsp;";
					}else if(value == 2){
						result += "<a href='javascript:queryPurInfo("+rowindex+ ")'>查看采购单</a>&nbsp;&nbsp;";
					}
					return result
				}
			});
	   }
	   gridArgs.columns = columns;
	   gridArgs.url = girdUrl;
       g = $("#maingrid").ligerGrid(gridArgs);
       
       function getAddData(){
   		var returnData = {};
   		var products = g.getData();
   		returnData.products = JSON.stringify(products);
   		if(isAddPage){ 
   			if($('#stockOutNo').val() != null){
   				returnData.stockOutNo = $('#stockOutNo').val()
   			}
   			if($('#receiverName').val() != null){
   				returnData.receiverName = $('#receiverName').val()
   			}
   			if($('#receiverTel').val() != null){
   				returnData.receiverTel = $('#receiverTel').val()
   			}
   			if($('#receiverAddr').val() != null){
   				returnData.receiverAddr = $('#receiverAddr').val()
   			}
   			if($('#stockOutType').val() != null){
   				returnData.stockOutType = $('#stockOutType').val()
   			}
   			if($('#stockOutTypeNo').val() != null){
   				returnData.stockOutTypeNo = $('#stockOutTypeNo').val()
   			}
   		}else{
   			if($('#stockOutNo').html() != null){
   				returnData.stockOutNo = $('#stockOutNo').html()
   			}
   			if($('#receiverName').html() != null){
   				returnData.receiverName = $('#receiverName').html()
   			}
   			if($('#receiverTel').html() != null){
   				returnData.receiverTel = $('#receiverTel').html()
   			}
   			if($('#receiverAddr').html() != null){
   				returnData.receiverAddr = $('#receiverAddr').html()
   			}
   			if($('#stockOutType').html() != null){
   				returnData.stockOutType = $('#stockOutType').html()
   			}
   			if($('#stockOutTypeNo').html() != null){
   				returnData.stockOutTypeNo = $('#stockOutTypeNo').html()
   			}
   		}
   		return returnData;
   	}
       
       function addStockOut(){
   		var isVail = true;
   	    if(isVail == true){
   			$.ajax({
   				async: false,
   				type: 'post',
   				dataType: 'text',
   				data:  getAddData(),
   				url: ajaxAddUrl,
   				success: function (data) {
   					if(data == 'fail'){
			     		  	window.parent.f_tip('出库失败');
			     	  }else{
							window.parent.f_tip('出库成功');
							operateSuccess();
			     	  }
   				  }
   			});   
   	    }
   	  }
       $().ready(function() {
    	   $("#form1").validate({
    			rules: {
					logisticsName: {required: true},
					logisticsOrder: {required: true}
				},
   	    		messages: {
   	    			logisticsOrder:{
   	    				required : '请输入物流编号'
   	    			},
   	    			logisticsName:{
   	   	    			required : '请选择物流公司'
   	   	    		}
   	    		},errorPlacement: function (lable, element){
		            if (element.hasClass("l-textarea")){
		            	try{
		            		lable.ligerHideTip();
		            		element.ligerTip({ content: lable.html(), target: element[0] });
	            		}catch(e){}
		            }else if (element.hasClass("l-text-field")){
		            	try{
		            		lable.ligerHideTip();
		            		element.parent().ligerTip({ content: lable.html(), target: element[0] });
	            		}catch(e){}
		            }else{
		                lable.appendTo(element.parents("td:first").next("td"));
		            }
		        },
		        success: function (lable)
		        {
		        	try{lable.ligerHideTip();}catch(e){}
		            lable.remove();
		        },
    	        submitHandler:function(form){
    	        	approveStockOut()
    	        }    
    	      }); 
      });     
    function checkProdcutBeforeApprove(){
    	var id = g.getData()[index].id;
  		$.post(
  			checkProdcutBeforeApproveUrl,
			{stockOutNo:stockOutNo},
			function(data) {
				if (data == 'success') {
					g.loadData(); 
					window.parent.parent.f_tip('删除成功');
				} else if (data == 'fail') {
					window.parent.parent.f_tip('删除失败');
				}
			}
  		);
    }   
   	function approveStockOut(){
   		var products = g.getData();
		$.ajax({
		       async: false,
		       type: 'post',
		       dataType: 'json',
		       data: sendData = {
   					stockOutNo:stockOutNo,
   					logisticsNo : $('#logisticsNo').val(),
   					logisticsName : $('#logisticsName').val(),
   					logisticsOrder : $('#logisticsOrder').val(),
   					productFetchType : $('#fetchbox').val(),
   					products : JSON.stringify(products)
   				},
		       url: ajaxApproveUrl,
		       success: function (data) {
		     	  if(data.result == 'fail'){
		     		  if(data.resultMsg !=  null && data.resultMsg != ""){
		     			window.parent.f_tip(data.resultMsg);
		     		  }else{
		     			window.parent.f_tip('出库失败');
		     		  }
		     	  }else{
						window.parent.f_tip('出库成功');
						operateSuccess();
		     	  }
		       }
		}); 
   	 }
  
   	function selectLogistics(){
	 	var url= basePath+'/stockmanage/StockController/logisticsSelect.do';
  		var m = $.ligerDialog.open({
				title:'选择物流',
				url:url,
				height:550,
				width:1024,
				isResize: false,
				isDrag: false,
				buttons: [
				          { text: '确定', onclick: f_selectOK },
				          { text: '取消', onclick: f_selectCancel }
				]
  			});
   		m.show(); 
   	}
	function f_selectOK(item, dialog){
		var fn = dialog.frame.getRowDate;
		data = fn();
		if (!data) {
			window.parent.f_tip('请选择物流公司!'); 
		}else{
			$("#logisticsName").val(data.logisticsName);
			$("#logisticsNo").val(data.logisticsId);
			dialog.close();
		}
	}
	function f_selectCancel(item, dialog){
		dialog.close();
	}
	
	 function queryImportCode(rowindex) { //查询条码
		 	var data = g.getSelectedRow();
			var url = barCodeQueryUrl + '?stockOutProductId='+ data.id+'&stockOutNo='+ stockOutNo;
			var m = $.ligerDialog.open({
					title:'查看条形码',
					url:url,
					height:550,
					width:1224,
					isResize: false,
					isDrag: false
			});
			m.show(); 
	 }
	 
	 var barCodeErrorData = null;
	 var tipText = null;
	 function importBarCode(index){ //导入条码
		var data =  g.getSelectedRow()
     	var sendData = {
				stockOutNo : stockOutNo,
				stockOutProductId :data.id,
				stockOutType : $('#stockOutType').html(),
				stockOutTypeNo : $('#stockOutTypeNo').html(),
				prodNo:data.prodNo,
				supNo : data.supNo,
				whNo : data.whNo,
				prodQuantity : data.stockoutQuantity
		};
   	  	var imgurl = $("#fileupload").val();
   	  	var extend = imgurl.substring(imgurl.lastIndexOf("."), imgurl.length);
      	if (extend != ".xlsx" && extend !=  "xls"){
			window.parent.f_tip("不支持文件类型后缀xls和xlsx外文件导入");
		}else{
       	  $.ligerDialog.waitting("正在导入中,请不要关闭当前页面..."); 
       	  var options = {
       		url : ajaxUploadUrl,
       		fileElementId: "fileupload",
       		secureuri: false,
       		data : sendData,
       		dataType: 'text',
			success: function(result){
				
				if(result.indexOf('data') > -1){
					result = result.substring(result.indexOf('{'),result.lastIndexOf('}')+1)
					var data = eval("("+result+")").data;
					if(data.errorReason == 1){
						window.parent.f_tip('条形码['+data.codeNo+']不存在');
					} else if(data.errorReason == 2) {
						window.parent.f_tip('条形码['+data.codeNo+']不属于该商品');
					} else if(data.errorReason == 3) {
						window.parent.f_tip('条形码['+data.codeNo+']不处于该仓库中');
					} else if(data.errorReason == 4) {
						window.parent.f_tip('条形码['+data.codeNo+']已出库');
					}
				}else if(result.indexOf('error') > -1){
					window.parent.f_tip('导入失败');
				}else if(result.indexOf('success') > -1){
					g.updateCell('productCodeState', 1, index); 
					window.parent.f_tip('导入成功');
				}else if(result.indexOf('prodQuantityFail') > -1){
					window.parent.f_tip('导入条形码数量不一致');
				}else {
					result = result.substring(result.indexOf('{'),result.lastIndexOf('}')+1)
					var data = eval("("+result+")").data;
					/* var correctCodes = data.correctCodes == null ? 0 : data.correctCodes.length;
					var stockedOutCodes = data.stockedOutCodes == null ? 0 : data.stockedOutCodes.length;
					var noExistsCodes = data.noExistsCodes == null ? 0 : data.noExistsCodes.length;
					var productCount = correctCodes 
									 + stockedOutCodes
									 + noExistsCodes;
					tipText = "导入条形码中共有" + productCount+"条.其中有:<br>";
					tipText += correctCodes == 0 ? "" : "&nbsp;<font style = 'color:blue'>" + correctCodes + "</font> 件商品条码符合出库条件;<br>";
					tipText += stockedOutCodes == 0 ? "" : "&nbsp;<font style = 'color:red'>" + stockedOutCodes + "</font> 件商品条码已在其他出库单内;<br>";
					tipText += noExistsCodes == 0 ? "" : "&nbsp;<font style = 'color:red'>" + noExistsCodes + "</font> 件商品条码不存在;<br>";
					tipText += "请更改错误条形码后重新上传。<br>点击【确认】查看详情,点击【取消】关闭弹窗。";
					openBarCodeErrorWindow(tipText,data); */
				}
				$.ligerDialog.closeWaitting();  
			}
       	  }; 
       	  	$.ajaxFileUpload(options);
         }
     }
	 function openBarCodeErrorWindow(tipText,data) { //打开新增窗口
		$.ligerDialog.confirm(tipText,function (type) {
			if(type == true){
				barCodeErrorData = data;
				var m = $.ligerDialog.open({
					url : basePath + '/stockmanage/productBarCodeContrller/toProductBarcodeError.do',
					height : 550,
					width : 1024,
					isResize : false,
					isDrag : false
				});
				m.show();
			}
		});
	 }
	 function deleteImportCode(index) { //删除条形码
		var id = g.getData()[index].id;
		$.ligerDialog.confirm('确定删除条形码',
		function(yes) {
			if (yes) {
				$.post(
 					barCodeDeleteUrl,
 					{stockOutProductId : id},
 					function(data) {
 						if (data == 'success') {
 							g.updateCell('productCodeState', 0, index); 
 							window.parent.parent.f_tip('删除成功');
 						} else if (data == 'fail') {
 							window.parent.parent.f_tip('删除失败');
 						}
 					}
				);
			};
		});
	 }
	 
	 
	 function getHwData(){  //得到仓库数据
     	$.ajax
         ({
             cache: false,
             async: false,
             type: 'post',
             dataType: 'json',
             data: {},
             url: basePath+"/base/wharehouse/ajaxWhareHouseList.do",
             success: function (data) {
             	whData = data.Rows;
             	var json =  [];
             	for (var i = 0; i < whData.length; i++) {
             		var json2 = {};
             		json2.whNo = whData[i].whNo;
             		json2.whName = whData[i].whName;
 					json[i] = json2;
 				}
             	whData = json;
             }
         });
     	return whData;
     }
	 
	 function toSelectPur(rowindex) { 
		 var data = g.getSelectedRow();
		 var url = purInfoSelectUrl 
		 				+ '?prentGridIndex=' + rowindex
		 				+ '&stockoutQuantity=' + data.stockoutQuantity 
		 				+ '&commoName=' + data.commoName 
		 				+ '&prodNo='+ data.prodNo 
		 				+ '&id='+ data.id 
		 				+ '&stockOutNo=' + stockOutNo
		 				+ '&stockOutType=' + $('#stockOutType').html() 
		 				+ '&stockOutTypeNo=' + $('#stockOutTypeNo').html() 
		 				+ '&whName=' + data.whName  
		 				+ '&whNo=' + data.whNo
		 				+ '&rowindex=' + rowindex;
		 var m = $.ligerDialog.open({
				title:'关联采购单',
				url:url,
				height:650,
				width:1224,
				isResize: false,
				isDrag: false
		});
		m.show(); 
	 }
	 function updateCellForProductCodeState(rowindex){
		 g.updateCell('productCodeState', 1, 1);
	 }
	 function queryPurInfo(rowindex) { //查看选择的采购单
			var data = g.getSelectedRow();
			var url = purQueryUrl + '?stockOutProductId='+ data.id + '&stockOutNo='+ stockOutNo;
			var m = $.ligerDialog.open({
					title:'查看选择的采购单',
					url:url,
					height:550,
					width:1224,
					isResize: false,
					isDrag: false
			});
			m.show(); 
	 }
	 
	 
	 function deletePurInfo(index) { //删除选择的采购单
	    	var id = g.getData()[index].id;
	    	$.ligerDialog.confirm('确定删除选择的采购单',
	    		function(yes) {if (yes) {
	    			$.post(
	    			purInfoDeleteUrl,
 					{stockOutProductId : id},
 					function(data) {
 						if (data == 'success') {
 							g.updateCell('productCodeState', 0, index); 
 							window.parent.parent.f_tip('删除成功');
 						} else if (data == 'fail') {
 							window.parent.parent.f_tip('删除失败');
 						}
 					}
	    			);
	    		};
	    	});
	    }
</script>
</body>
</html>